require 'digest/sha1'

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def self.sha1(pass)
    Digest::SHA1.hexdigest("laphrasedelamort#{pass}quitue")
  end

  def self.generate_key
    str = ''; 30.times { str += (rand(26)+65).chr }; str
  end

  def my_text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
    if(tag_options[:id])
      tag_name = "#{tag_options[:id]}"
    else
      tag_name = "#{object}_#{method}"
    end

    (completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
        text_field(object, method, tag_options) +
        content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
        auto_complete_field(tag_name, { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
  end
  
  def render_state(task)
    case task.state
    when 'folder'
      image_tag('folder.gif')
    else
      "<div class='state #{task.state}'>#{task.state}</div>"
    end
  end
  
  # used in print. Maybe to be refactored
  def sort_tasks(tasks)
    tasks.sort_by { |t| [t.state, t.order] }
  end
  
end

class MyLinkRenderer < WillPaginate::LinkRenderer

  def page_link(page, text, attributes = {})
    @template.link_to_remote(text, {:url=>{:page=>page}}, attributes)
  end

end

